package org.example.mislugares;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class Mapa extends FragmentActivity implements OnInfoWindowClickListener {
private GoogleMap mapa;

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapa);
    mapa = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.mapa)).getMap();
    mapa.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mapa.setMyLocationEnabled(true);
    mapa.getUiSettings().setZoomControlsEnabled(true);
    mapa.getUiSettings().setCompassEnabled(true);
    if (Lugares.vectorLugares.size() > 0) {
        GeoPunto p = Lugares.vectorLugares.get(0).getPosicion();
        mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(p.getLatitud(), p.getLongitud()), 12));
    }

    for (Lugar lugar : Lugares.vectorLugares) {
        GeoPunto p = lugar.getPosicion();
        if (p != null && p.getLatitud() != 0) {
            BitmapDrawable iconoDrawable = (BitmapDrawable) getResources()
                    .getDrawable(lugar.getTipo().getRecurso());
            Bitmap iGrande = iconoDrawable.getBitmap();
            Bitmap icono = Bitmap.createScaledBitmap(iGrande,
                    iGrande.getWidth() / 7, iGrande.getHeight() / 7, false);
            mapa.addMarker(new MarkerOptions()
                        .position(new LatLng(p.getLatitud(), p.getLongitud()))
                        .title(lugar.getNombre()).snippet(lugar.getDireccion())
                        .icon(BitmapDescriptorFactory.fromBitmap(icono)));
            }

        }
    
    mapa.setOnInfoWindowClickListener(this);
    
    mapa.setOnMapLongClickListener(new OnMapLongClickListener() {
    	public void onMapLongClick(LatLng point) {
    	Toast.makeText(
    	Mapa.this,
    	"Lat: " + point.latitude + "\n" +
    	"Lng: " + point.longitude + "\n",
    	Toast.LENGTH_SHORT).show();
    	}
    	});

    }

@Override public void onInfoWindowClick(Marker marker) {
    for (int id=0; id<Lugares.vectorLugares.size(); id++){
        if (Lugares.vectorLugares.get( id).getNombre().equals(marker.getTitle())){
            Intent intent = new Intent(this, VistaLugar.class);
            intent.putExtra("id", (long)id);
            startActivity(intent);
            break;
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.mapa, menu);            
      return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.mostrarcoordenadas:
    	  lanzarInfoCoordenadas(null);
    	  break;
    }
    
    return false;
    
}

public void lanzarInfoCoordenadas(View view){
    Intent i = new Intent(this, InfoCoordenadas.class);
          startActivity(i);
  }

}
